var user;
var pwd;
var num;
$(function() {
    $("#login").click(function() {
        user = $("#user").val();
        pwd = $("#pwd").val();
        num = 1;
        if (user == "") {
            swal("登陆失败", "账号不能为空", "error");
        } else if (pwd == "") {
            swal("登陆失败", "密码不能为空", "error");
        } else {
            ajax("login");
        }

    });
    $("#sign_in").click(function() {
        user = $("#user").val();
        pwd = $("#pwd").val();
        num = 3;
        ajax("regiester");
    });
});

function ajax(type) {
    $.ajax({
        url: "/blackhouse/login",
        type: "post",
        data: {
            "id": user,
            "pwd": pwd,
            "num": num
        },
        dataType: "json",
        success: function(e) {
            showInfo(e, type);
        }
    })
}

function showInfo(array, type) {
    if (type == "login") {
        if (array == 1) {
            window.location.href="http://localhost:8080/blackhouse/static/html/Administrators.html";
        } else if (array == 2) {
            window.location.href="http://localhost:8080/blackhouse/static/html/Teacherinterface.html";
        } else if (array == 3) {
            window.location.href = "http://localhost:8080/blackhouse/static/html/Studentinterface.html";
        } else {
            swal("登陆失败", "账号或密码错误", "error");
        }
    } else if (type == "regiester") {
        if (array > 0) {
            alert("注册成功");
        } else {
            alert("注册失败");
        }
    }
}